package com.haiheng.voiceandbook.utils

import android.content.Context
import android.text.Editable


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2021/11/22 0022
 * @Version: 1.0
 */
object SpUtil {


    fun getString(context: Context, key: String, def: String = "") =
        context.getSharedPreferences("${context.packageName}_sp", Context.MODE_PRIVATE)
            .getString(key, def) ?: def


    fun putString(context: Context, key: String, value: String) {
        val sp = context.getSharedPreferences("${context.packageName}_sp",Context.MODE_PRIVATE).edit()
        sp.putString(key,value)
        sp.apply()
//        context.getSharedPreferences("", Context.MODE_PRIVATE).edit{
//            putString(context,key,value)
//            apply()
//        }
    }

    fun putLong(context: Context, key: String, value: Long) {
        val sp = context.getSharedPreferences("${context.packageName}_sp",Context.MODE_PRIVATE).edit()
        sp.putLong(key,value)
        sp.apply()
    }
    fun putInt(context: Context, key: String, value: Int) {
        val sp = context.getSharedPreferences("${context.packageName}_sp",Context.MODE_PRIVATE).edit()
        sp.putInt(key,value)
        sp.apply()
//        context.getSharedPreferences("${context.packageName}_sp", Context.MODE_PRIVATE).edit {
//            putInt(key, value)
//            apply()
//        }
    }

    fun getInt(context: Context, key: String, def: Int = 0) =
        context.getSharedPreferences("${context.packageName}_sp", Context.MODE_PRIVATE)
            .getInt(key, def)

    fun getLong(context: Context, key: String, def: Long = 0) =
        context.getSharedPreferences("${context.packageName}_sp", Context.MODE_PRIVATE)
            .getLong(key, def)
    fun putBoolean(context: Context, key: String, value: Boolean) {
        val sp = context.getSharedPreferences("${context.packageName}_sp",Context.MODE_PRIVATE).edit()
        sp.putBoolean(key,value)
        sp.apply()
    }

    fun getBoolean(context: Context, key: String, def: Boolean = false) =
        context.getSharedPreferences("${context.packageName}_sp", Context.MODE_PRIVATE)
            .getBoolean(key, def)
}

